{*
  Object Pascal simple BDE Stored Procedures manipulation routines
  @author  Maciej Zagozda (1998-2008)
  @version 2008/04/20 Added documentation support
  @version 2008/05/11 Added DisposeParameters function
  @version 2008/05/12 Modified AppendParameter functions
}

unit StoredProcRoutines;

interface

uses
  DB,
  DBTables,
  SysUtils,
  Classes,
  Contnrs;

type
  TStoredProcedureParamType = (spptString, spptDate);

procedure ClearParameters(var aStoredProcedure: TStoredProc);
procedure DisposeParameters(var aStoredProcedure: TStoredProc);
function ProcessParamName(const strName: AnsiString): AnsiString;

procedure AppendParameter(var aStoredProcedure: TStoredProc; const strName, strValue: AnsiString); overload;
procedure AppendDateParameter(var aStoredProcedure: TStoredProc; const strName: AnsiString; const dtmValue: TDateTime); 
procedure AppendParameter(var aStoredProcedure: TStoredProc; const strName: AnsiString; const intValue: Longint); overload;
procedure AppendFloatParameter(var aStoredProcedure: TStoredProc; const strName: AnsiString; const fltValue: Double); 

implementation

{*
  clear stored procedure parameters
*}

procedure ClearParameters(var aStoredProcedure: TStoredProc);
var
  intA: Integer;
begin
  for intA := 0 to aStoredProcedure.ParamCount - 1 do
  begin
    aStoredProcedure.Params[intA].Clear;
    aStoredProcedure.Params[intA].Bound := False;
  end;
end;

{*
  Destroys parameters objects on a TStoredProc object
*}

procedure DisposeParameters(var aStoredProcedure: TStoredProc);
begin
  aStoredProcedure.Params.Clear;
end;

{*
  checks if the parameter starts with a valid symbol
*}

function ProcessParamName( const strName: AnsiString ): AnsiString;
begin
  Result := strName;
  if ( Length( strName ) > 0 ) and ( Copy( strName, 1, 1 ) <> '@' ) then Result := '@' + strName;
end;

{*
  creates a parameter on provided TStoredProc and populates it with ftString value provided
*}

procedure AppendParameter( var aStoredProcedure: TStoredProc; const strName, strValue: AnsiString );
begin
  with aStoredProcedure do
  begin
    Params.CreateParam(ftString, ProcessParamName(strName), ptInput);
    Params.ParamByName(ProcessParamName(strName)).AsString := strValue;
  end;//with
end;

{*
  creates a parameter on provided TStoredProc and populates it with ftDateTime value provided
*}

procedure AppendDateParameter( var aStoredProcedure: TStoredProc; const strName: AnsiString; const dtmValue: TDateTime );
begin
  with aStoredProcedure do
  begin
    Params.CreateParam(ftDateTime, ProcessParamName(strName), ptInput);
    Params.ParamByName(ProcessParamName(strName)).AsDate := dtmValue;
  end;//with
end;

{*
  creates a parameter on provided TStoredProc and populates it with ftInteger value provided
*}

procedure AppendParameter( var aStoredProcedure: TStoredProc; const strName: Ansistring; const intValue: Longint );
begin
  with aStoredProcedure do
  begin
    Params.CreateParam(ftInteger, ProcessParamName(strName), ptInput);
    Params.ParamByName(ProcessParamName(strName)).AsInteger := intValue;
  end;//with
end;

{*
  creates a parameter on provided TStoredProc and populates it with ftFloat value provided
*}

procedure AppendFloatParameter( var aStoredProcedure: TStoredProc; const strName: AnsiString; const fltValue: Double );
begin
  with aStoredProcedure do
  begin
    Params.CreateParam(ftFloat, ProcessParamName(strName), ptInput);
    Params.ParamByName(ProcessParamName(strName)).AsFloat := fltValue;
  end;//with
end;
end.
